package com.gitee.free.coding.core.utils;

import java.util.Random;

/**
 * 验证码生成器
 */
public class CaptchaGenerator {

    /**
     * 基础文本
     */
    public static final String BASE_TEXT = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    /**
     * 生成4位随机文本
     *
     * @return 4位随机文本
     */
    public static String generateRandomText4() {
        return generateRandomText(4);
    }

    /**
     * 生成随机文本
     *
     * @param length 生成的随机文本长度
     * @return 随机文本
     */
    public static String generateRandomText(int length) {
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(BASE_TEXT.length());
            char ch = BASE_TEXT.charAt(number);
            sb.append(ch);
        }
        return sb.toString();
    }

}
